package com.fz.controller;

import com.fz.entity.Category;
import com.fz.service.CategoryService;
import com.fz.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author fz
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    CategoryService categoryService;

    @GetMapping("/list")
    ResultVo<List<Category>> list() {
        return ResultVo.success(categoryService.getList());
    }

    @PostMapping("/del")
    ResultVo<Boolean> del(Long id) {
        return ResultVo.success(categoryService.removeById(id), "删除类别成功");
    }

    @PostMapping("/add")
    ResultVo<Boolean> add(String name, Long parentId, Integer level, String description) {
        return ResultVo.success(categoryService.save(new Category(name,parentId,level,description)), "添加类别成功");
    }

    @GetMapping("/getById")
    ResultVo<Category> getById(Long id) {
        return ResultVo.success(categoryService.getById(id));
    }

    @PostMapping("update")
    ResultVo<Boolean> update(Long id, String name, Long parentId, Integer level, String description) {
        return ResultVo.success(categoryService.updateById(new Category(id,name,parentId,level,description)), "修改类别成功");
    }
}
